package com.douyin.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class UserAgentUtil {
    
    private static final Pattern MOBILE_PATTERN = Pattern.compile("(?i)mobile|android|iphone|ipad|phone|mobi|fennec|minimo|symbian|psp|nintendo|blackberry|playbook|kindle|silk|webOS|palm|opera mini", Pattern.CASE_INSENSITIVE);
    private static final Pattern TABLET_PATTERN = Pattern.compile("(?i)tablet|ipad|playbook|kindle|silk", Pattern.CASE_INSENSITIVE);
    
    public static Map<String, String> parseUserAgent(String userAgent) {
        Map<String, String> result = new HashMap<>();
        
        if (userAgent == null || userAgent.trim().isEmpty()) {
            result.put("deviceType", "unknown");
            result.put("browserName", "unknown");
            result.put("browserVersion", "unknown");
            result.put("osName", "unknown");
            result.put("osVersion", "unknown");
            return result;
        }
        
        // 设备类型识别
        String deviceType = "desktop";
        if (TABLET_PATTERN.matcher(userAgent).find()) {
            deviceType = "tablet";
        } else if (MOBILE_PATTERN.matcher(userAgent).find()) {
            deviceType = "mobile";
        }
        result.put("deviceType", deviceType);
        
        // 浏览器识别
        String browserName = "unknown";
        String browserVersion = "unknown";
        
        // Chrome
        Pattern chromePattern = Pattern.compile("Chrome/(\\d+\\.\\d+)");
        Matcher chromeMatcher = chromePattern.matcher(userAgent);
        if (chromeMatcher.find() && !userAgent.contains("Edg") && !userAgent.contains("OPR")) {
            browserName = "Chrome";
            browserVersion = chromeMatcher.group(1);
        }
        // Safari
        else if (userAgent.contains("Safari") && !userAgent.contains("Chrome")) {
            browserName = "Safari";
            Pattern safariPattern = Pattern.compile("Version/(\\d+\\.\\d+)");
            Matcher safariMatcher = safariPattern.matcher(userAgent);
            if (safariMatcher.find()) {
                browserVersion = safariMatcher.group(1);
            }
        }
        // Firefox
        else if (userAgent.contains("Firefox")) {
            browserName = "Firefox";
            Pattern firefoxPattern = Pattern.compile("Firefox/(\\d+\\.\\d+)");
            Matcher firefoxMatcher = firefoxPattern.matcher(userAgent);
            if (firefoxMatcher.find()) {
                browserVersion = firefoxMatcher.group(1);
            }
        }
        // Edge
        else if (userAgent.contains("Edg")) {
            browserName = "Edge";
            Pattern edgePattern = Pattern.compile("Edg/(\\d+\\.\\d+)");
            Matcher edgeMatcher = edgePattern.matcher(userAgent);
            if (edgeMatcher.find()) {
                browserVersion = edgeMatcher.group(1);
            }
        }
        // Opera
        else if (userAgent.contains("OPR")) {
            browserName = "Opera";
            Pattern operaPattern = Pattern.compile("OPR/(\\d+\\.\\d+)");
            Matcher operaMatcher = operaPattern.matcher(userAgent);
            if (operaMatcher.find()) {
                browserVersion = operaMatcher.group(1);
            }
        }
        // WeChat
        else if (userAgent.contains("MicroMessenger")) {
            browserName = "WeChat";
            Pattern wechatPattern = Pattern.compile("MicroMessenger/(\\d+\\.\\d+)");
            Matcher wechatMatcher = wechatPattern.matcher(userAgent);
            if (wechatMatcher.find()) {
                browserVersion = wechatMatcher.group(1);
            }
        }
        
        result.put("browserName", browserName);
        result.put("browserVersion", browserVersion);
        
        // 操作系统识别
        String osName = "unknown";
        String osVersion = "unknown";
        
        // Windows
        if (userAgent.contains("Windows NT")) {
            osName = "Windows";
            Pattern windowsPattern = Pattern.compile("Windows NT (\\d+\\.\\d+)");
            Matcher windowsMatcher = windowsPattern.matcher(userAgent);
            if (windowsMatcher.find()) {
                String version = windowsMatcher.group(1);
                switch (version) {
                    case "10.0": osVersion = "10"; break;
                    case "6.3": osVersion = "8.1"; break;
                    case "6.2": osVersion = "8"; break;
                    case "6.1": osVersion = "7"; break;
                    default: osVersion = version; break;
                }
            }
        }
        // macOS
        else if (userAgent.contains("Mac OS X")) {
            osName = "macOS";
            Pattern macPattern = Pattern.compile("Mac OS X (\\d+[._]\\d+)");
            Matcher macMatcher = macPattern.matcher(userAgent);
            if (macMatcher.find()) {
                osVersion = macMatcher.group(1).replace("_", ".");
            }
        }
        // iOS
        else if (userAgent.contains("iPhone OS") || userAgent.contains("iPad")) {
            osName = "iOS";
            Pattern iosPattern = Pattern.compile("OS (\\d+[._]\\d+)");
            Matcher iosMatcher = iosPattern.matcher(userAgent);
            if (iosMatcher.find()) {
                osVersion = iosMatcher.group(1).replace("_", ".");
            }
        }
        // Android
        else if (userAgent.contains("Android")) {
            osName = "Android";
            Pattern androidPattern = Pattern.compile("Android (\\d+\\.\\d+)");
            Matcher androidMatcher = androidPattern.matcher(userAgent);
            if (androidMatcher.find()) {
                osVersion = androidMatcher.group(1);
            }
        }
        // Linux
        else if (userAgent.contains("Linux")) {
            osName = "Linux";
        }
        
        result.put("osName", osName);
        result.put("osVersion", osVersion);
        
        return result;
    }
}